package
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.PerspectiveProjection;
	import flash.ui.Keyboard;

	import mx.utils.StringUtil;

	public class TAF_KEY_test extends Sprite
	{
		static private var KEYDOWN:Vector.<Boolean> = new Vector.<Boolean>(230, true);

		static private var KEYDOWNEACH:Vector.<Vector.<Function>> = new Vector.<Vector.<Function>>(230, true);

		static private var KEYDOWNED:Vector.<Boolean> = new Vector.<Boolean>(230, true);

		static private var KEYUP:Vector.<Boolean> = new Vector.<Boolean>(230, true);

		static private var lastKEYUP:int;

		private var KEYCODE:Object = {'0': '0'.charCodeAt(),
				'1': '1'.charCodeAt(),
				'2': '2'.charCodeAt(),
				'3': '3'.charCodeAt(),
				'4': '4'.charCodeAt(),
				'5': '5'.charCodeAt(),
				'6': '6'.charCodeAt(),
				'7': '7'.charCodeAt(),
				'8': '8'.charCodeAt(),
				'9': '9'.charCodeAt(),
				'~': 192,
				'`': 192,
				'-': 189,
				'_': 189,
				'=': 187,
				'+': 187,
				'[': 219,
				'{': 219,
				']': 221,
				'}': 221,
				'|': 220,
				'\\': 220,
				':': 186,
				';': 186,
				'"': 222,
				'\'': 222,
				',': 188,
				'<': 188,
				'.': 190,
				'>': 190,
				'/': 191,
				'?': 191};


		public function TAF_KEY_test()
		{
			//http://www.diebuster.com/?p=1597

			stage.addEventListener(KeyboardEvent.KEY_DOWN, KEY_DOWN);
			stage.addEventListener(KeyboardEvent.KEY_UP, KEY_UP);
			keyADD({'space & f': [hnSpace, hnFire], 'left & a & b': hnLeft, 'right & d': hnRight, 'up & w': hnJump, 'down & s': hnFall});
		}

		private function keyADD($keyName$functionName:*):void
		{
			var keyCode:String = "";
			var regExp:RegExp = /\(.&\)/g;
			var keyNameArr:Array = []

			for (var keyName:String in $keyName$functionName)
			{
				keyCode = "";
				keyNameArr = [];
				keyNameArr = keyName.split("&");
				for (var i:int = 0; i < keyNameArr.length; i++)
				{
					keyCode = StringUtil.trim(keyNameArr[i]).toUpperCase(); //대문자로 변경

					if (keyCode in Keyboard)
					{
						keyCode = Keyboard[keyCode];
					}
					else
					{
						keyCode = KEYCODE[keyCode];
					}

					var tempFn:Vector.<Function> = new Vector.<Function>;

					if ($keyName$functionName[keyName] is Array)
					{
						//실행 할 함수가 다수일 경우
						for each (var fn:Function in $keyName$functionName[keyName])
						{
							tempFn[tempFn.length] = fn;
						}
					}
					else
					{
						//실행 할 함수가 1개일 경우
						tempFn[0] = $keyName$functionName[keyName];
					}

					KEYDOWNEACH[keyCode] = tempFn;
				}
			}
		}

		private function KEY_DOWN($event:KeyboardEvent):void
		{
			var code:int, runner:*;
			code = $event.keyCode;

			if (KEYDOWN[code])
			{
			}
			else
			{
				KEYDOWN[code] = true;
				KEYDOWNED[code] = true;

				if (KEYDOWNEACH[code])
				{
					if (KEYDOWNEACH[code].length)
					{
						for each (runner in KEYDOWNEACH[code])
						{
							runner();
						}
					}
				}
			}
			$event.stopImmediatePropagation();
		}

		private function KEY_UP($event:KeyboardEvent):void
		{
			var code:int;
			code = $event.keyCode;

			KEYUP[lastKEYUP] = false;
			lastKEYUP = code;
			KEYUP[lastKEYUP] = true;
			KEYDOWN[lastKEYUP] = false;
			KEYDOWNED[lastKEYUP] = false;
			$event.stopImmediatePropagation();
		}

		private function KEY_ENTER($event:Event):void
		{
			var runner:*;
		/*for each (runner in RUNNER[0])
		   {
		   runner();
		 }*/
		}

		private function keyHandler_F():void
		{
			trace("keyHandler_F");
		}

		private function hnFire():void
		{
			trace("hnFire");
		}

		private function hnSpace():void
		{
			trace("hnSpace");
		}

		private function hnLeft():void
		{
			trace("hnLeft");
		}

		private function hnRight():void
		{
			trace("hnRight");
		}

		private function hnJump():void
		{
			trace("hnJump");
		}

		private function hnFall():void
		{
			trace("hnFall");
		}
	}
}